<?php
require('header.php');
require('paginator.class.php');
requireAuth();

$facebook = new Facebook($fbconfig);
$fbUserId = $facebook->getUser();
$authUrl = 'http://goo.gl/ruDCvm';

?>
			<script type="text/javascript">
				$(document).ready(function(){
					$('#btn-accesstoken').click(function(){
						window.open('view-source:<?php echo $authUrl; ?>');
					});
					$('#btn-auth-token').click(function(){
						window.open('<?php echo $authUrl; ?>');
					});
					$('#btn-newtoken').click(function(){
						$('#gettoken').toggle();
					});
					$('#btn-save').click(function(){
						$('.modal-body').html('');
						var targetid = $('#targetid').val(), numoflike = $('#numoflike').val(), access_token = $('#access_token').val(), btn = $(this);
						if(targetid == ''){
							$('#targetid').focus();
							return false;
						}
						if(numoflike == ''){
							$('#numoflike').focus();
							return false;
						}
						if(access_token == ''){
							$('#access_token').focus();
							return false;
						}
						$('.modal-body').html('Loading..');
						btn.button('loading');

						$.ajax({
							url: '<?php echo route('do-like') ?>',
							dataType: 'json',
							type: 'POST',
							data: {targetid:targetid,numoflike:numoflike,access_token:access_token},
							success: function(rs){
								if(typeof rs != 'undefined'){
									var fbdata = '<h3 class="pull-left" style="margin-top:1px;margin-right: 3px;"><span class="label label-success">Success: '+rs.success.length+'</span></h3> <h3 style="margin-top:1px;" class="pull-left"><span class="label label-danger">Error: '+rs.error.length+'</span></h3><div class="clearfix"></div><div class="bg-warning" style="max-height:300px; overflow:auto;padding: 5px;">';
									if(rs.error.length > 0){
										fbdata += '<fieldset><legend>Error logs:</legend>';
										for(i = 0; i < rs.error.length; i++){
											fbdata += '<small class="small">- '+rs.error[i]+'</small><br />';
										}
										fbdata += '</fieldset>';
									}
									$('.modal-body').html(fbdata + '</div>');
								}
								else{
									$('.modal-body').html('Error when posting data.');
								}
								btn.button('reset');
							},
							error: function(xhr, status, err){
								btn.button('reset');
								$('.modal-body').html("Error when posting data<br />" + xhr.responseText);
								btn.button('reset');
							}
						});
					});
				});
			</script>
			<div class="row">
				<div class="col-md-8">
					<div class="panel panel-default">
					  <!-- Default panel contents -->
					  <div class="panel-heading"><h4>Mass Facebook Like</h4></div>
					  <!-- Table -->
					  <div class="container-fluid" style="margin:10px 0;">
						  <form class="form-horizontal" role="form">
								<div class="form-group">
								    <label for="access_token" class="col-sm-4 control-label">Token URL</label>
								    <div class="col-sm-8">
								      <input type="text" name="access_token" class="form-control" id="access_token" placeholder="https://www.facebook.com/connect/login_success.html#access_token=CAAAACZAVC6ygBABZAYbB4aKV2cbctg2DK82JSHqmZAmkxrSSBbzyOTlVuwtLzqALD9ge9tBgLHAJpGkHvvZBpeTqh5Hu5DXquhEeMUfl1eZAxFs4KNhbTJ9NsoWmZCooaeYLdhQZBYD5aRxCksjohZAe5d9l1Ri5eXIU1nK" value="<?php echo $t = !empty($_SESSION['cs_access_token']) ? 'https://www.facebook.com/connect/login_success.html#access_token=' . $_SESSION['cs_access_token'] : ''; ?>">

										<?php if(!empty($_SESSION['cs_access_token'])): ?><p><button style="margin-top:5px;" type="button" id="btn-newtoken" class="btn btn-success btn-xs">Get new access token</button></p><?php endif; ?>
										<div class="well" id="gettoken" style="margin-top:5px;<?php echo $toggle = !empty($_SESSION['cs_access_token']) ? 'display:none;' : '' ?>">
									      <p>Don't have access token? Following these steps below:</p>
									      <ol>
											<li style="margin-bottom:5px;"><button type="button" id="btn-auth-token" class="btn btn-success btn-xs">Authorize permissions</button></li>
											<li><button type="button" id="btn-accesstoken" class="btn btn-success btn-xs">Get the Access token</button></li>
									      </ol>

								    	</div>

								    </div>
								</div>
								<div class="form-group">
								    <label for="targetid" class="col-sm-4 control-label">Enter Facebook Post/Photo ID</label>
								    <div class="col-sm-8">
								      <input type="text" name="targetid" class="form-control" id="targetid" value="">
								    </div>
								</div>
								<div class="form-group">
								    <label for="numoflike" class="col-sm-4 control-label">Expected number of like</label>
								    <div class="col-sm-8">
								      <input type="number" name="numoflike" class="form-control" id="numoflike" value="0" style="width:80px;"> <em class="small">Input 0 to get maximum like count</em>
								    </div>
								</div>
								

								<div class="form-group">
							    <div class="col-sm-offset-4 col-sm-8">
							      <button type="button" data-backdrop="static" data-keyboard="false" id="btn-save" class="btn btn-primary" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-thumbs-up"></span> Submit</button>
							    </div>
							  </div>
						  </form>
					  </div>
					</div>
				</div>
			</div>


			<div class="modal fade" id="myModal">
			<div class="modal-dialog">
			<div class="modal-content">
			  <div class="modal-header">
			    <button type="button" class="close page-reload" data-dismiss="modal" aria-hidden="true">&times;</button>
			    <h4 class="modal-title">Result</h4>
			  </div>
			  <div class="modal-body">
			    
			  </div>
			  <div class="modal-footer">
			    <button type="button" class="btn btn-default page-reload" data-dismiss="modal">Close</button>
			  </div>
			</div><!-- /.modal-content -->
			</div><!-- /.modal-dialog -->
			</div><!-- /.modal -->
<?php require('footer.php'); ?>